﻿
layui.use(['form','table','element','upload','layer'], function(){
    //直接渲染表格
    var form = layui.form;
    var table = layui.table;
    var upload = layui.upload;
    var element = layui.element;
    var $ = layui.$;
    //印刷
    not_approved();//默认请求未审批的

    function approved(){

        table.render({
            //质检部未审批的
            elem: '#printingTable'
            ,headers: {
                Accept: "application/json; charset=utf-8",
            },toolbar: ['<p>',
                '<button lay-event="approved" class="layui-btn layui-btn-sm icon-btn"><i class="layui-icon">&#xe605;</i>已审批</button>&nbsp;',
                '<button lay-event="not_approved" class="layui-btn layui-btn-sm layui-btn-danger icon-btn layui-bg-blue"><i class="layui-icon">&#xe68d;</i>待审批</button>',
                '&nbsp;&nbsp;印刷已审批表格</p>'
            ].join('')
            ,height: 700
            ,where :{
                //未审批
                status:1
            }
            ,url: '/general/testzs/public/printing/Inspector/InspectorList'
            ,cols: [[ //表头
                { title: '序号', width:80, type:'numbers', sort: true, fixed: 'left'},
                { title: 'printing_id', hide:true, }
                ,{field: 'printing_number', title: '申请单号'}
                ,{field: 'customer_name', title: '客户名称'}
                ,{field: 'order_number', title: '订单号'}
                ,{field: 'user_name', title: '制单人'}
                ,{field: 'create_time', title: '发单时间'}
                ,{field: 'tips', title: '当前状态'}
                ,{field:'right', title: '操作',toolbar:"#PrintingTool"}
                ,{ title: '采购状态',field:'purchase_status',  hide:true, }
                ,{ title: '总经办状态',field:'general_status', hide:true, }
                ,{ title: '质检部状态',field:'inspector_status', hide:true, }
                ,{ title: '采购审批时间',field:'purchase_time',  hide:true, }
                ,{ title: '总经办审批时间',field:'general_time', hide:true, }
                ,{ title: '质检部审批时间',field:'inspector_time', hide:true, }
            ]]
            ,response: {
                statusName: 'code' //规定数据状态的字段名称，默认：code
                , statusCode: 200 //规定成功的状态码，默认：0
                , msgName: 'msg' //规定状态信息的字段名称，默认：msg
                , countName: 'count' //规定数据总数的字段名称，默认：count
                , dataName: 'data' //规定数据列表的字段名称，默认：data
            }
            ,page: true
            ,text: { none: '暂无印刷记录' }
        });
    }

    function not_approved(){
        table.render({
            //采购部未审批的
            elem: '#printingTable'
            ,headers: {
                Accept: "application/json; charset=utf-8",
            },toolbar: ['<p>',
                '<button lay-event="approved" class="layui-btn layui-btn-sm icon-btn"><i class="layui-icon">&#xe605;</i>已审批</button>&nbsp;',
                '<button lay-event="not_approved" class="layui-btn layui-btn-sm layui-btn-danger icon-btn layui-bg-blue"><i class="layui-icon">&#xe68d;</i>待审批</button>',
                '&nbsp;&nbsp;印刷已审批表格</p>'
            ].join('')
            ,height: 700
            ,where :{
                //未审批
                status:2
            }
            ,url: '/general/testzs/public/printing/inspector/InspectorList'
            ,cols: [[ //表头
                { title: '序号', width:80, type:'numbers', sort: true, fixed: 'left'},
                { title: 'printing_id', hide:true, }
                ,{field: 'printing_number', title: '申请单号'}
                ,{field: 'customer_name', title: '客户名称'}
                ,{field: 'order_number', title: '订单号'}
                ,{field: 'user_name', title: '制单人'}
                ,{field: 'create_time', title: '发单时间'}
                ,{field: 'tips', title: '当前状态'}
                ,{field:'right', title: '操作',toolbar:"#NotPrintingTool"}
                ,{ title: '采购状态',field:'purchase_status',  hide:true, }
                ,{ title: '总经办状态',field:'general_status', hide:true, }
                ,{ title: '质检部状态',field:'inspector_status', hide:true, }
                ,{ title: '采购审批时间',field:'purchase_time',  hide:true, }
                ,{ title: '总经办审批时间',field:'general_time', hide:true, }
                ,{ title: '质检部审批时间',field:'inspector_time', hide:true, }
            ]]
            ,response: {
                statusName: 'code' //规定数据状态的字段名称，默认：code
                , statusCode: 200 //规定成功的状态码，默认：0
                , msgName: 'msg' //规定状态信息的字段名称，默认：msg
                , countName: 'count' //规定数据总数的字段名称，默认：count
                , dataName: 'data' //规定数据列表的字段名称，默认：data
            }
            ,page: true
            ,text: { none: '暂无印刷记录' }
        });
    }

    /* 表格头工具栏点击事件-已审批&&待审批 */
    table.on('toolbar(printingTable)', function (obj) {
        // 已审批
        if (obj.event === 'approved') {
            approved();
        }
        // 待审批
        else if (obj.event === 'not_approved') {
            not_approved();
        }
    });

    table.on('tool(NotarticlesDetail)', function (obj) {
       if (obj.event === 'edit_edition') {
            //修改

        }
    });

    //条事件
    table.on('tool(printingTable)', function(obj){
        var data = obj.data;
        if(obj.event === 'detail'){
            //打开详情
            layer.open({
                type: 1,
                area: ['1000px','800px'],
                offset: '100px',
                title: data.user_name+'的印刷详情',
                content: $('#PrintingDetailHtml').html(), // 注意这里有.html()
                success: function(layero, dIndex) {
                    //查询类别 渲染上去
                    //渲染 不让操作

                    $('#customer_name_view').val(data.customer_name); //data.printing_number
                    $('#order_number_view').val(data.order_number); //data.printing_number
                    $('#remarks_view').val(data.remarks);
                    $('#purchase_content_view').val(data.purchase_content); //data.printing_number
                    $('#inspector_content_view').val(data.inspector_content);
                    $("#printing_price_view").val(data.printing_price);
                    $("#currency_view").val(data.currency);
                    $("input[name='is_collect_cost'][value='"+ data.is_collect_cost +"']").prop("checked", "checked");
                    //审批下列
                    form.render();
                    //审批下列
                    $('#user_name').val(data.user_name); //data.printing_number
                    $('#create_time').val(data.create_time); //data.printing_number

                    if (data.purchase_status === 2){
                        $('#purchase_tips').val('未审批');
                    }else if (data.purchase_status === 1){
                        $('#purchase_tips').val('通过');
                        $('#purchase_time').val(data.purchase_time);
                    }else if (data.purchase_status === 3){
                        $('#purchase_tips').val('拒绝');
                        $('#purchase_time').val(data.purchase_time);
                    }


                    if (data.general_status === 2){
                        $('#general_tips').val('未审批');
                    }else if (data.general_status === 1){
                        $('#general_tips').val('通过');
                        $('#general_time').val(data.general_time);
                    }else if (data.general_status === 3){
                        $('#general_tips').val('拒绝');
                        $('#general_time').val(data.general_time);
                    }
                    if (data.inspector_status === 2){
                        $('#inspector_tips').val('未审批');
                    }else if (data.inspector_status === 1){
                        $('#inspector_tips').val('通过');
                        $('#inspector_time').val(data.inspector_time);
                    }else if (data.inspector_status === 3){
                        $('#inspector_tips').val('拒绝');
                        $('#inspector_time').val(data.inspector_time);
                    }
                    //请求附件
                    $('#purchase_content').val(data.purchase_content)

                    //是否驳回
                    if (data.status === 5){
                        $('#reject_status').show();
                        $('#reject_content_view').val(data.reject_content)
                    }
                    //已审批内展示文件
                    $.ajax({
                        type: "POST",
                        url: '/general/testzs/public/printing/index/PrintingFileAll',
                        data: {
                            'printing_id':data.printing_id
                        },
                        traditional: true,
                        dataType:"json",
                        success: function(data){
                            if (data.code === 200){
                                $('#file_content').html(data.data);
                            }else{

                            }
                        }
                    })


                    table.render({
                        elem: '#articlesDetail'
                        ,height: 312
                        ,where:{
                            printing_id:data.printing_id
                        }
                        ,headers: {
                            Accept: "application/json; charset=utf-8",
                        }
                        ,url: '/general/testzs/public/printing/index/PrintingArticles' //正常表
                        ,cols: [[ //表头
                            { title: '序号', width:80, type:'numbers', sort: true, fixed: 'left'},
                            { title: 'articles_id', hide:true, }
                            ,{field: 'value', title: '类别名称', width:150}
                            ,{field: 'name', title: '名称', width:150}
                            ,{field: 'articles_content', title: '内容', width:200}
                            ,{field: 'special', title: '特殊要求及备注', width:150}
                            ,{field: 'articles_edition', title: '印刷版号'}
                            ,{field: 'user_file', title: '附件', width:150}
                        ]]
                        ,response: {
                            statusName: 'code' //规定数据状态的字段名称，默认：code
                            , statusCode: 200 //规定成功的状态码，默认：0
                            , msgName: 'msg' //规定状态信息的字段名称，默认：msg
                            , countName: 'count' //规定数据总数的字段名称，默认：count
                            , dataName: 'data' //规定数据列表的字段名称，默认：data
                        }
                        ,page: true
                    });

                },end : function() {
                    //回调后结束操作
                }
            });
        }
        if(obj.event === 'detail_not'){
            //打开详情
            layer.open({
                type: 1,
                area: ['1000px','800px'],
                offset: '100px',
                title: data.user_name+'的印刷详情',
                content: $('#NotPrintingDetailHtml').html(), // 注意这里有.html()
                success: function(layero, dIndex) {
                    //查询类别 渲染上去
                    //渲染 不让操作
                    //回头这里改成请求的
                    $('#customer_name_view_not').val(data.customer_name); //data.printing_number
                    $('#order_number_view_not').val(data.order_number); //data.printing_number
                    $('#remarks_view_not').val(data.remarks);
                    $("#printing_price_view_not").val(data.printing_price);
                    $("#currency_view_not").val(data.currency);
                    $("input[name='is_collect_cost_not'][value='"+ data.is_collect_cost +"']").prop("checked", "checked");
                    //审批下列
                    form.render();
                    $('#user_name_not').val(data.user_name); //data.printing_number
                    $('#create_time_not').val(data.create_time); //data.printing_number
                    if (data.purchase_status === 2){
                        $('#purchase_tips_not').val('未审批');
                    }else if (data.purchase_status === 1){
                        $('#purchase_tips_not').val('通过');
                        $('#purchase_time_not').val(data.purchase_time);
                    }else if (data.purchase_status === 3){
                        $('#purchase_tips_not').val('拒绝');
                        $('#purchase_time_not').val(data.purchase_time);
                    }
                    if (data.general_status === 2){
                        $('#general_tips_not').val('未审批');
                    }else if (data.general_status === 1){
                        $('#general_tips_not').val('通过');
                        $('#general_time_not').val(data.general_time);
                    }else if (data.general_status === 3){
                        $('#general_tips_not').val('拒绝');
                        $('#general_time_not').val(data.general_time);
                    }
                    if (data.inspector_status === 2){
                        $('#inspector_tips_not').val('未审批');
                    }else if (data.inspector_status === 1){
                        $('#inspector_tips_not').val('通过');
                        $('#inspector_time_not').val(data.inspector_time);
                    }else if (data.inspector_status === 3){
                        $('#inspector_tips_not').val('拒绝');
                        $('#inspector_time_not').val(data.inspector_time);
                    }

                    $('#purchase_content').val(data.purchase_content)
                    uploadAll(data.printing_id);
                    //请求附件
                    table.render({
                        elem: '#NotarticlesDetail'
                        ,height: 312
                        ,where:{
                            printing_id:data.printing_id
                        }
                        ,headers: {
                            Accept: "application/json; charset=utf-8",
                        }
                        ,url: '/general/testzs/public/printing/index/PrintingArticles' //正常表
                        ,cols: [[ //表头

                            { title: '序号', width:80, type:'numbers', sort: true, fixed: 'left'},
                            { title: 'articles_id', hide:true, }
                            ,{field: 'value', title: '类别名称', width:150}
                            ,{field: 'name', title: '名称', width:150}
                            ,{field: 'articles_content', title: '内容', width:150}
                            ,{field: 'special', title: '特殊要求及备注', width:150}
                            ,{field: 'articles_edition', title: '印刷版号(点击可新增)',edit: 'text',width:200}
                            ,{field: 'user_file', title: '附件', width:150}
                            //,{align: 'center', title: '操作', templet: '#status', width: 200},
                        ]]
                        ,response: {
                            statusName: 'code' //规定数据状态的字段名称，默认：code
                            , statusCode: 200 //规定成功的状态码，默认：0
                            , msgName: 'msg' //规定状态信息的字段名称，默认：msg
                            , countName: 'count' //规定数据总数的字段名称，默认：count
                            , dataName: 'data' //规定数据列表的字段名称，默认：data
                        }
                        ,page: true
                    });
                    //质检部审批
                    var modify_value
                    layui.table.on('edit(NotarticlesDetail)', function (obj) {
                        console.log(obj);
                        modify_value = obj.value; //修改后的数据
                        //失焦时间直接保存
                        layer.confirm('您要保存该条印刷版号吗?', function(index){
                            $.ajax({
                                type: "POST",
                                url: '/general/testzs/public/printing/Inspector/CommitArticlesEdition',
                                data: {
                                    'articles_edition':modify_value,
                                    'articles_id':obj.data.articles_id
                                },
                                traditional: true,
                                dataType:"json",
                                success: function(data){
                                    if (data.code === 200){
                                        layer.alert(data.msg);
                                        setTimeout(() => {
                                            table.reload('NotarticlesDetail')
                                        }, 500);
                                    }else{
                                        layer.alert(data.msg);
                                    }
                                }
                            })
                            layer.close(index);
                        });
                    })

                    $('#InspectorConfirm').click(function () {
                        var inspector_content  = $('#inspector_content').val();
                        if (inspector_content === ''){
                            layer.alert('请填写质检部确认内容！');
                            return false;
                        }
                        layer.confirm('确认通过吗?', function(index){
                            var loading = layer.load(0, {
                                shade: false,
                                time: 2*1000
                            });
                            $.ajax({
                                type: "POST",
                                url: '/general/testzs/public/printing/Inspector/InspectorConfirm',
                                data: {
                                    'inspector_content':inspector_content,
                                    'printing_id':data.printing_id,
                                    'status': 1
                                },
                                traditional: true,
                                dataType:"json",
                                success: function(data){
                                    layer.close(loading);
                                    if (data.code === 200){
                                        layer.alert(data.msg);
                                        layer.closeAll();
                                        setTimeout(() => {
                                            table.reload('printingTable')
                                        }, 500);
                                    }else{
                                        layer.alert(data.msg);
                                    }
                                }
                            })
                            layer.close(index);
                        });


                    })
                    $('#InspectorRefuseConfirm').click(function () {
                        layer.open({
                            type: 1,
                            area: '1100px',
                            offset: '65px',
                            title: '填写驳回原因',
                            content: $('#refuseForm').html(),
                            success: function (layero,index) {
                                // 驳回提交点击事件
                                $("#refuseCommit").click(function () {
                                    var refuse = $("#refuse").val();
                                    var loading = layer.load(0, {
                                        shade: false,
                                        time: 2*1000
                                    });
                                    $.ajax({
                                        url: '/general/testzs/public/printing/Inspector/InspectorConfirm',
                                        type: "POST",
                                        dataType: "json",
                                        data: {
                                            // 登陆的id
                                            'printing_id':data.printing_id,
                                            'status':3,
                                            'reject_content':refuse
                                        },
                                        headers: {
                                            Accept: "application/json; charset=utf-8",
                                        },
                                        success: function (res) {
                                            layer.close(loading);
                                            if (res.code === 200) {
                                                layer.msg(res.msg, {
                                                    icon: 1
                                                });
                                                setTimeout(function () {
                                                    // 关闭所有弹窗
                                                    layer.closeAll();
                                                    setTimeout(() => {
                                                        table.reload('printingTable')
                                                    }, 500);
                                                }, 1000);
                                            } else {
                                                layer.msg(res.msg, {
                                                    icon: 2
                                                });
                                            }
                                        },
                                    })
                                })
                                // 驳回取消点击事件
                                $("#refuseClear").click(function () {
                                    setTimeout(function () {
                                        layer.close(index);
                                    }, 500);
                                })
                            }
                        });
                    })

                },end : function() {
                    //回调后结束操作
                }


            });
        }

    });

    function uploadAll(printing_id){

        var uploadListIns = upload.render({
            elem: '#testList'
            ,elemList: $('#demoList') //列表元素对象
            ,url: '/general/testzs/public/printing/Inspector/InspectorUpload'
            ,accept: 'file'
            ,data:{
                'printing_id':printing_id
            }
            ,multiple: true
            ,number: 3
            ,auto: false
            ,bindAction: '#testListAction'
            ,choose: function(obj){
                var that = this;
                var files = this.files = obj.pushFile(); //将每次选择的文件追加到文件队列
                //读取本地文件
                obj.preview(function(index, file, result){
                    var tr = $(['<tr id="upload-'+ index +'">'
                        ,'<td>'+ file.name +'</td>'
                        ,'<td>'+ (file.size/1014).toFixed(1) +'kb</td>'
                        ,'<td><div class="layui-progress" lay-filter="progress-demo-'+ index +'"><div class="layui-progress-bar" lay-percent=""></div></div></td>'
                        ,'<td>'
                        ,'<button class="layui-btn layui-btn-xs demo-reload layui-hide">重传</button>'
                        ,'<button class="layui-btn layui-btn-xs layui-btn-danger demo-delete">删除</button>'
                        ,'</td>'
                        ,'</tr>'].join(''));

                    //单个重传
                    tr.find('.demo-reload').on('click', function(){
                        obj.upload(index, file);
                    });

                    //删除
                    tr.find('.demo-delete').on('click', function(){
                        delete files[index]; //删除对应的文件
                        tr.remove();
                        uploadListIns.config.elem.next()[0].value = ''; //清空 input file 值，以免删除后出现同名文件不可选
                    });

                    that.elemList.append(tr);
                    element.render('progress'); //渲染新加的进度条组件
                });
            }
            ,done: function(res, index, upload){ //成功的回调
                var that = this;
                if(res.code === 200){ //上传成功
                    layer.alert('上传成功');
                    var tr = that.elemList.find('tr#upload-'+ index)
                        ,tds = tr.children();
                    tds.eq(3).html(''); //清空操作
                    delete this.files[index]; //删除文件队列已经上传成功的文件
                    return;
                }
                this.error(index, upload);
            }
            ,allDone: function(obj){ //多文件上传完毕后的状态回调
                console.log(obj)
            }
            ,error: function(index, upload){ //错误回调
                var that = this;
                var tr = that.elemList.find('tr#upload-'+ index)
                    ,tds = tr.children();
                tds.eq(3).find('.demo-reload').removeClass('layui-hide'); //显示重传
            }
            ,progress: function(n, elem, e, index){ //注意：index 参数为 layui 2.6.6 新增
                element.progress('progress-demo-'+ index, n + '%'); //执行进度条。n 即为返回的进度百分比
            }
        });
    }



});

